package com.tdw.eurekaclient.controller;

import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.tdw.eurekaclient.service.HystrixService;

@RestController
@RequestMapping(value="/a")
public class HelloController {
	private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
	@Autowired
	private DiscoveryClient client;
	
	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private HystrixService hystrixService;
	
	@RequestMapping(value="/hello")
	public String hello(){
		ServiceInstance instance = client.getLocalServiceInstance();
		logger.info("####host={},port={},serviceId={}",instance.getHost(),instance.getPort(),instance.getServiceId());
		return "ok";
	}
	
	
	/*
	 * getForEntity("http://USER-SERVICE/user?name= {l}", String.class, "jack")
	 * getForEntity("http://USER-SERVICE/user?name= {name}", String.class, map)// <name,jack>...
	 */
	
	@RequestMapping(value="/printParm")
	public String printParm(HttpServletRequest request, HttpServletResponse response){
		ServiceInstance instance = client.getLocalServiceInstance();
		logger.info("####host={},port={},serviceId={}",instance.getHost(),instance.getPort(),instance.getServiceId());
		StringBuffer parms = new StringBuffer();
		Map<String, String[]> parmMap = request.getParameterMap();
		
		for(Entry<String, String[]> entry : parmMap.entrySet()){
			String parmKey = entry.getKey();
			parms.append(parmKey).append("={").append(parmKey).append("}&");
		}
		parms = parms.deleteCharAt(parms.length()-1);
		logger.info("parms={}",parmMap);
		
		return "ok";
	}
	
	@RequestMapping(value="/hystrix/test")
	public String hystrixTest(HttpServletRequest request, HttpServletResponse response){
		ServiceInstance instance = client.getLocalServiceInstance();
		logger.info("####host={},port={},serviceId={}",instance.getHost(),instance.getPort(),instance.getServiceId());
		StringBuffer parms = new StringBuffer();
		Map<String, String[]> parmMap = request.getParameterMap();
		
		for(Entry<String, String[]> entry : parmMap.entrySet()){
			String parmKey = entry.getKey();
			parms.append(parmKey).append("={").append(parmKey).append("}&");
		}
		parms = parms.deleteCharAt(parms.length()-1);
		return hystrixService.hystrixTest(parms.toString(),parmMap);
	}
	
	
	@RequestMapping(value="/hystrix/fallback")
	@HystrixCommand(fallbackMethod = "hystrixTestFallback")
	public String hystrixFallBackTest(HttpServletRequest request, HttpServletResponse response){
		Map<String, String[]> parmMap = request.getParameterMap();
		
		for(Entry<String, String[]> entry : parmMap.entrySet()){
			logger.info("====parm:key={},value={}",entry.getKey(),entry.getValue());
		}
		String exception = request.getParameter("err");
		if("exception".equals(exception)){
			throw new RuntimeException("test exception");
		}
		try {
			Thread.sleep(2500);
		} catch (InterruptedException e) {
			logger.error("失败",e);
		}
		return "ok";
	}
	
	public String hystrixTestFallback(HttpServletRequest request, HttpServletResponse response,Throwable e){
		assert "test exception".equals(e.getMessage());
		
		logger.info("------>to hystrixTestFallback");
		return "hystrixTestFallback";
	}
	
}
